package com.lf.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lf.DAO.courseDAO;
import com.lf.DAO.teacherDAO;
import com.lf.entity.shiti.course;
import com.lf.entity.shiti.teacher;
import com.lf.impl.cou_impl;
import com.lf.impl.tea_impl;

/**
 * Servlet implementation class course_servlet
 */
@WebServlet("/course_Servlet")
public class course_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public course_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if(action==null){
			findAll(request,response);
			
		}
		switch (action) {
		case "findAll":
			findAll(request,response);
			break;
		case "find_tea_name":
			find_tea_name(request,response);
			break;
		case "find_course_name":
			find_course_name(request,response);
			break;
		case"add_cou":
			add_cou(request,response);
			break;
		case"update_cou":
			update_cou(request,response);
			break;
		case"delect_cou":
			delect_cou(request,response);
			break;
		default:
			break;
		}
	}

	


	private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		System.out.print("asdasd");
		courseDAO cDao = new cou_impl();
		List<course> list = cDao.findAll();
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test001.jsp");
	}
	
	private void find_tea_name(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String t_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
	
		System.out.println(t_id);
		courseDAO cDao = new cou_impl();
		List<course> name_list = cDao.find_tea_name(t_id);
		request.getSession().setAttribute("name_list", name_list);
		response.sendRedirect("test001.jsp");
	}
	private void find_course_name(HttpServletRequest request, HttpServletResponse response) throws IOException{

		String c_name =new String(request.getParameter("c_name").getBytes(        "ISO-8859-1"), "utf-8") ;

		System.out.println(c_name);
		courseDAO cDao = new cou_impl();
		List<course> name_list = cDao.find_course_name(c_name);
		request.getSession().setAttribute("name_list", name_list);
		response.sendRedirect("test002.jsp");

	}
	
	private void add_cou(HttpServletRequest request, HttpServletResponse response) throws IOException{
	
		String c_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		String c_name =new String(request.getParameter("c_name").getBytes(        "ISO-8859-1"), "utf-8") ;
		String t_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		System.out.println(c_id);
		System.out.println(c_name);
		System.out.println(t_id);
		courseDAO cDao = new cou_impl();
		int count = cDao.add_cou(c_id, c_name, t_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
		
		response.sendRedirect("test001.jsp");
	}
	
	private void update_cou(HttpServletRequest request, HttpServletResponse response) throws IOException{
		System.out.print("asda");
		
		String c_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		String c_name =new String(request.getParameter("c_name").getBytes(        "ISO-8859-1"), "utf-8") ;
		String t_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");
		System.out.println(t_id);
		System.out.println(c_name);
		System.out.println(t_id);
		courseDAO cDao = new cou_impl();
		int count = cDao.update_cou(c_name,t_id,c_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
		
		response.sendRedirect("test001.jsp");
		
	}
	
	private void delect_cou(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String c_id = new String(request.getParameter("t_id").getBytes(        "ISO-8859-1"), "utf-8");

		System.out.println(c_id);
		courseDAO cDao = new cou_impl();
		int count = cDao.delect_cou(c_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
		
		response.sendRedirect("test001.jsp");
	}
}
